package com.car.loan.project.xr.controller.after;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.file.ExcelExportUtil;
import com.car.loan.common.file.ExcelUtil;
import com.car.loan.common.utils.file.FileUtils;
import com.car.loan.framework.aspectj.lang.annotation.DataStatus;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.controller.ExcelFileUtil;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.service.OverdueService;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.bo.business.after.OverdueImportLogBo;
import com.car.loan.project.xr.util.bo.business.after.OverdueInputBo;
import com.car.loan.project.xr.util.select.LoanBasePageParam;
import com.car.loan.project.xr.util.vo.after.OverdueImportLogVo;
import com.car.loan.project.xr.util.vo.after.OverdueInputVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author huangding
 * @date 2020/2/21 20:44
 */
@RestController
@RequestMapping("xr/overdue")
@Api(value = "逾期管理", tags = "贷后管理-逾期管理")
public class OverdueController {

    @Autowired
    private OverdueService overdueService;

    @Resource
    private ExcelFileUtil excelFileUtil;

    @Autowired
    protected HttpServletRequest request;


    @Autowired
    private ILoanService loanService;


    @PreAuthorize("@ss.hasPermi('overdue:import:excel')")
    @PostMapping("overdueImport")
    @ApiOperation("逾期导入")
    @Log(title = "逾期导入", businessType = BusinessType.IMPORT)
    public synchronized AjaxResult overdueImport(@RequestParam("file") MultipartFile file,
        @ApiParam("用银行id") @RequestParam("bankId") Long bankId,
        @ApiParam("数据日期 yyy-mm-dd") @RequestParam("dataDate") String dataDate,
        @ApiParam("数据更新模式 1=更新所有数据  2=更新导入数据") @RequestParam("pattern") Integer pattern)
        throws IOException {
        try {
            LocalDate parse = LocalDate.parse(dataDate);
            LocalDate now = LocalDate.now();
            if (parse.compareTo(now) > 0) {
                return AjaxResult.error("数据日期不可大于当前日期");
            }
        } catch (Exception e) {
            return AjaxResult.error("数据日期时间格式错误");
        }
        if (pattern > 2 || pattern < 0) {
            return AjaxResult.error("数据更新模式格式错误");
        }
        if (FileUtils.fileNameIsExcel(file.getOriginalFilename())) {
            List<List<Object>> excelInfo = ExcelUtil
                .getExcelInfo(file.getInputStream(), ExcelUtil.isExcel2003(file.getName()));
            if (null == excelInfo || excelInfo.size() == 0) {
                return AjaxResult.error("无数据");
            }
            return overdueService.overdueImport(excelInfo, bankId, dataDate, pattern);
        }
        return AjaxResult.error("文件格式错误，只支持.xlsx，.xls格式");
    }

    @PreAuthorize("@ss.hasPermi('overdue:import:list')")
    @GetMapping("overdueImportLog/list")
    @ApiOperation(value = "逾期导入记录", response = OverdueImportLogVo.class, responseContainer = "list")
    public AjaxResult overdueImportLog(@Valid OverdueImportLogBo overdueImportLogBo) {
        return AjaxResult.success(overdueService.getOverdueImportLog(overdueImportLogBo));
    }

    @PreAuthorize("@ss.hasPermi('overdue:import:list')")
    @GetMapping("overdueImportLog/excel")
    @ApiOperation("逾期导入记录-excel")
    public AjaxResult overdueImportLogExcel(@Valid OverdueImportLogBo overdueImportLogBo,
        HttpServletResponse response) {
        overdueImportLogBo.setPageNum(1);
        overdueImportLogBo.setPageSize(99999999);
        Page<OverdueImportLogVo> overdueImportLog = overdueService
            .getOverdueImportLog(overdueImportLogBo);
        InputStream templateExcelFile = excelFileUtil
            .getTemplateInputStream("overdue_log.xlsx");
        String exportFileName = "逾期导入记录" + LocalDate.now() + ".xlsx";
        ExcelExportUtil
            .exportByBeans(2, templateExcelFile, exportFileName, false, null,
                overdueImportLog.getRecords(),
                request, response);
        return null;
    }


    @PreAuthorize("@ss.hasPermi('overdue:input:input')")
    @PostMapping("overdue/input")
    @ApiOperation("逾期录入")
    @Log(title = "逾期录入", businessType = BusinessType.IMPORT)
    public AjaxResult overdueInput(@RequestBody OverdueInputBo overdueInputBo) {
        try {
            LocalDate parse = LocalDate.parse(overdueInputBo.getDataDate());
            LocalDate now = LocalDate.now();
            if (parse.compareTo(now) > 0) {
                return AjaxResult.error("数据日期不可大于当前日期");
            }
        } catch (Exception e) {
            return AjaxResult.error("数据日期时间格式错误");
        }
        return overdueService.overdueInput(overdueInputBo);
    }


    @PreAuthorize("@ss.hasPermi('overdue:input:list')")
    @GetMapping("/overdue/input/list")
    @ApiOperation(value = "逾期录入列表页", response = LoanBaseInfo.class, responseContainer = "list")
    @DataStatus(loanStatusEnum = com.car.loan.common.enums.LoanStatusEnum.OVERDUE_INPUT_LIST)
    public AjaxResult overdueInputList(@Valid LoanBasePageParam loanBasePageParam) {
        return AjaxResult.success(loanService.page(loanBasePageParam));
    }

    @PreAuthorize("@ss.hasPermi('overdue:input:query')")
    @GetMapping("/overdue/input/{loanId}")
    @ApiOperation(value = "逾期录入详情页", response = OverdueInputVo.class)
    public AjaxResult overdueInputInfo(@PathVariable Long loanId) {
        return AjaxResult.success(overdueService.getOverdueInputInfo(loanId));
    }


}
